package com.choosefine.base.shixiaobao.device.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.base.shixiaobao.base.BaseController;
import com.choosefine.base.shixiaobao.common.def.CommonConstant;
import com.choosefine.base.shixiaobao.common.def.ResultCode;
import com.choosefine.base.shixiaobao.device.model.LedInfo;
import com.choosefine.base.shixiaobao.device.service.LedInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author xiongchao 2017/7/19
 * @category led详情
 */
@RestController
@RequestMapping("/ledinfo")
@Api(value = "led详情", description = "熊朝")
public class LedInfoController extends BaseController {
	Logger logger = LoggerFactory.getLogger(LedInfoController.class);

	@Autowired
	LedInfoService ledInfoService;

	@ApiOperation(value = "根据led主键编号获取led详情", notes = "传入led主键编号，根据led主键编号获取led详情")
	@RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
	public String getLedInfoById(@PathVariable Integer id) throws Exception {
		String result = null;
		if (id != null) {
			LedInfo ledInfo = ledInfoService.getLedInfoById(id);
			result = responseSuccess(ledInfo);
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}

	@ApiOperation(value = "保存led详情", notes = "保存led详情")
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST)
	public String saveLedInfo(@Valid @RequestBody LedInfo ledInfo) throws Exception {
		String result = null;
		if (ledInfo != null) {
			ledInfo.setCtime(System.currentTimeMillis());
			ledInfo.setIsValid(CommonConstant.VALID);
			Integer id = ledInfoService.save(ledInfo);
			if (id != null)
				result = responseSuccess(id);
			else
				result = responseFail(ResultCode.MSG_ERROR,
						messageSourceUtils.getMessage("attendance.exception.msg_save_fail"));
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}

	@ApiOperation(value = "更新led详情", notes = "更新led详情")
	@RequestMapping(method = RequestMethod.PUT)
	public String updateLedInfo(@RequestBody LedInfo ledInfo) throws Exception {
		String result = null;
		if (ledInfo != null) {
			ledInfo.setUtime(System.currentTimeMillis());
			Integer num = ledInfoService.update(ledInfo);
			if (num != null)
				result = responseSuccess(num);
			else
				result = responseFail(ResultCode.MSG_ERROR,
						messageSourceUtils.getMessage("attendance.exception.msg_update_fail"));
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}

	@ApiOperation(value = "删除led详情", notes = "删除led详情")
	@RequestMapping(value = "id/{id}", method = RequestMethod.DELETE)
	public String deleteLedInfo(@PathVariable Integer id) throws Exception {
		String result = null;
		if (id != null) {
			Integer num = ledInfoService.delete(id);
			if (num != null && num > 0)
				result = responseSuccess(num);
			else
				result = responseFail(ResultCode.MSG_ERROR,
						messageSourceUtils.getMessage("attendance.exception.msg_delete_fail"));
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}
}
